#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
#  Uruk Update Manager
#  
#  Copyright 2022 hayder majid <hayder@riseup.net>
#  
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#  
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#  
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#  MA 02110-1301, USA.


import os
import signal
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk as gtk
try:
    gi.require_version('AyatanaAppIndicator3', '0.1')
    from gi.repository import AyatanaAppIndicator3 as appindicator
except ValueError:
    gi.require_version('AppIndicator3', '0.1')
    from gi.repository import AppIndicator3 as appindicator
gi.require_version('Gtk', '3.0')

APPINDICATOR_ID = 'urukupdater'
app_ico = '/usr/share/urukUpdater/pixmap/logo/logo.svg'

def main():
    indicator = appindicator.Indicator.new(APPINDICATOR_ID, os.path.abspath(app_ico), appindicator.IndicatorCategory.SYSTEM_SERVICES)
    indicator.set_status(appindicator.IndicatorStatus.ACTIVE)
    indicator.set_menu(build_menu())
    gtk.main()

def build_menu():
    menu = gtk.Menu()
    # software_cen = gtk.MenuItem('Software Center')
    # software_cen.connect('activate', software_center)
    # menu.append(software_cen) 
    status_sys = gtk.MenuItem('Status')
    status_sys.connect('activate', status_s)
    menu.append(status_sys) 
    update_sys = gtk.MenuItem('Update System')
    update_sys.connect('activate', update_system)
    menu.append(update_sys)
    about_app = gtk.MenuItem('Preferences')
    about_app.connect('activate', settings)
    menu.append(about_app)
    quit_app = gtk.MenuItem('Quit')
    quit_app.connect('activate', quit)
    menu.append(quit_app)
    menu.show_all()
    return menu


# def software_center(source):
    # os.system('pkexec /usr/sbin/synaptic')
def status_s(source):
    os.system('/usr/lib/urukUpdater/status-sys')
def update_system(source):
    os.system('sudo /usr/lib/urukUpdater/checker | zenity --title="System Updating Info" --text="Checking your System for updates \n \n this may take a few minutes depending on your speed connection...." --no-cancel --progress --pulsate --width=400 --height=140 --auto-kill --auto-close --window-icon=/usr/share/urukUpdater/pixmap/wait.svg')
    os.system('/usr/share/urukUpdater/updater-status')
def settings(source):
    os.system('python3 /usr/share/urukUpdater/uruk-conf.py')
def quit(source):
    os.system('/usr/lib/urukUpdater/end_proc')
    gtk.main_quit()
if __name__ == "__main__":
    signal.signal(signal.SIGINT, signal.SIG_DFL)
main()
